#include <iostream>
#include <pthread.h>
#include <unistd.h>

using namespace std;


class Request
{
public:
    Request(int start, int end, const string &threadname)
    : start_(start), end_(end), threadname_(threadname)
    {}
public:
    int start_;
    int end_;
    string threadname_;
};

class Response
{
public:
    Response(int result, int exitcode):result_(result),exitcode_(exitcode)
    {}
public:
    int result_;   // 计算结果
    int exitcode_; // 计算结果是否可靠
};

void *sumCount(void *args) // 线程的参数和返回值，不仅仅可以用来进行传递一般参数，也可以传递对象！！
{
    Request *rq = static_cast<Request*>(args); //  Request *rq = (Request*)args
    Response *rsp = new Response(0,0);
    for(int i = rq->start_; i <= rq->end_; i++)
    {
        cout << rq->threadname_ << " is runing, caling..., " << i << endl;
        rsp->result_ += i;
        usleep(100000);
    }
    delete rq;
    return rsp;
}

int main()
{
    pthread_t tid;
    Request *rq = new Request(1, 100, "thread 1");
    pthread_create(&tid, nullptr, sumCount, rq);


    void *ret;
    pthread_join(tid, &ret);
    Response *rsp = static_cast<Response *>(ret);
    cout << "rsp->result: " << rsp->result_ << ", exitcode: " << rsp->exitcode_ << endl;
    delete rsp;
    return 0;
}





// void *threadRun(void* args)
// {
//     while(1)
//     {
//         std::cout << "new thread: " << getpid() << std::endl;
//         sleep(1);
//     }
//     return nullptr;
// }

// int main()
// {
//     pthread_t tid;
//     pthread_create(&tid, nullptr, threadRun, nullptr);


//     while(1)
//     {
//         std::cout << "main thread: " << getpid() << std::endl;
//         sleep(1);
//     }
// }